<?php 


if ($_POST['is_submitted']) {
	
	$error = 0;
	if(!$_POST['priceE']) $error  = 1;
	if(!$_POST['priceE']) $error  = 1;
	if(!$_POST['country-name']) $error  = 1;
	
	/*$sql = "SELECT * FROM countries WHERE name = '".escape($_POST['country-name'])."' AND zone = '".intval($_POST['country'])."'";
	$result = query($sql);
	if(!$row = mysql_fetch_object($result)) $error  = 1;
	*/
	
	if(!$error)
	{
			
		if ($_POST['update']) {
			
			
			
			$order_id = intval($_POST['update']);
			
			$sql = "UPDATE orders SET
					  price = '".escape($_POST['priceE'])."',
					  order_date = '".date('Y-m-d')."',
					  iass = '".intval($_POST['iass'])."',
					  culinary = '".intval($_POST['culinary'])."',
					  dinner = '".intval($_POST['dinner'])."',
					  allergic = '".intval($_POST['allergic'])."'
					 WHERE
					 order_id = '$order_id'
					";
			query($sql);
			
			
			
			
			
			$_SESSION['order']['order_id'] = $order_id;
			$_SESSION['order']['price'] = number_format($_POST['priceE'],2);
			$_SESSION['order']['priceD'] = number_format($_POST['priceD'],2);
			
			$_SESSION['order']['culinary'] = $_POST['culinary'];
			$_SESSION['order']['dinner'] = $_POST['dinner'];
			$_SESSION['order']['iass'] = $_POST['iass'];
			
			 # PayPal request
			
				$paypal = new SetExpressCheckout("".$_SESSION['order']['price']."");	
				$paypal->setNVP("DESC", "12th world Congress of Semiotics Sofia 2014, 16-20 September. New Bulgarian University(Registration #".number_pad($order_id,5).")");
							
				$paypal->getResponse();	
			
		}else{
			
			
			$_SESSION['first_name'] = $_POST['first_name'];
		$_SESSION['last_name'] = $_POST['last_name'];
		# Create Order
			$sql = "INSERT INTO orders
					  (firstname, lastname, email, country, continent,institution, price, iass, culinary, dinner,	allergic,byear,lang)
					VALUES
					  ('".escape($_POST['first_name'])."','".escape($_POST['last_name'])."','".escape($_POST['email'])."','".escape($_POST['country-name'])."','".intval($_POST['country'])."','".escape($_POST['institution'])."','".escape($_POST['priceE'])."','".intval($_POST['iass'])."','".intval($_POST['culinary'])."','".intval($_POST['dinner'])."','".intval($_POST['allergic'])."','".intval($_POST['year'])."','".$_SESSION['lang']."')";
			query($sql);
			$order_id = mysql_insert_id();
			
			$_SESSION['country-name'] = $_POST['country-name'];
			$_SESSION['country'] = $_POST['country'];
			$_SESSION['USER_EMAIL'] = $_POST['email'];
			$_SESSION['first_name'] = $_POST['first_name'];
			$_SESSION['last_name'] = $_POST['last_name'];
			
			$_SESSION['order']['order_id'] = $order_id;
			$_SESSION['order']['price'] = number_format($_POST['priceE'],2);
			$_SESSION['order']['priceD'] = number_format($_POST['priceD'],2);
			
			$_SESSION['order']['culinary'] = $_POST['culinary'];
			$_SESSION['order']['dinner'] = $_POST['dinner'];
			$_SESSION['order']['iass'] = $_POST['iass'];
			
		 # Mail to Admin
			/*mail( ADMINISTRATOR_EMAIL,
				  sprintf(ADMIN_ORDER_SUBJECT, number_pad($order_id, 5)), 
				  sprintf(ADMIN_ORDER_MESSAGE, $_POST['first_name'], $_POST['last_name']),
				  HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
				  $_SESSION['order']['order_id'] = $order_id;
				  $_SESSION['order']['price'] = number_format($_POST['priceE'],2);
				  $_SESSION['order']['priceD'] = number_format($_POST['priceD'],2);*/
			# Mail to User
			$pay_url = HTML_BASE.$_SESSION['lang'].'/registration-form/'.base64_encode($order_id);
			
			if($_POST['country'] == 3)
			{
				
				$add = '';
				$price = $_SESSION['order']['price'];
				
				if($_SESSION['order']['iass']){
					
					$add .= __MESSAGE_3_IASS;
					
					if($_POST['country-name'] == 'ARGENTINA' || $_POST['country-name'] == 'VENEZUELA, BOLIVARIAN REPUBLIC OF')
					{
						$price -= 25;
					}else{
						$price -= 15;
					}
					
				}
				
				if($_SESSION['order']['culinary']){
					
					$add .= __MESSAGE_3_Culinary;
					$price -= 35;
				}
				
				if($_SESSION['order']['dinner']){
					
					$add .= __MESSAGE_3_dinner;
					$price -= 35;
				}
				
				
			mail( $_SESSION['USER_EMAIL'], 
				  USER_ORDER_SUBJECT,
				  sprintf(USER_ORDER_3_MESSAGE, 
				  $_POST['first_name'], $_POST['last_name'],
				  date('d/m/Y - H:m'),
				  $_POST['first_name'], $_POST['last_name'],
				  $_POST['email'], $_POST['year'],
				  $_POST['country-name'], $_POST['institution'],
				  $pay_url,$pay_url,$price,$add), 
				  HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
				  
			 mail( ADMINISTRATOR_EMAIL, 
				  USER_ORDER_SUBJECT,
				  sprintf(USER_ORDER_3_MESSAGE, 
				  $_POST['first_name'], $_POST['last_name'],
				  date('d/m/Y - H:m'),
				  $_POST['first_name'], $_POST['last_name'],
				  $_POST['email'], $_POST['year'],
				  $_POST['country-name'], $_POST['institution'],
				  $pay_url,$pay_url,$price,$add), 
				  HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
				  
				  # PayPal request
			
				$paypal = new SetExpressCheckout("".$_SESSION['order']['price']."");	
				$paypal->setNVP("DESC", "12th world Congress of Semiotics Sofia 2014, 16-20 September. New Bulgarian University(Registration #".number_pad($order_id,5).")");
							
				$paypal->getResponse();	
			}else{
				
				 
				 mail( $_SESSION['USER_EMAIL'], 
				  USER_ORDER_SUBJECT,
				  sprintf(USER_ORDER_MESSAGE, 
				  $_POST['first_name'], $_POST['last_name'],
				  date('d/m/Y - H:m'),
				  $_POST['first_name'], $_POST['last_name'],
				  $_POST['email'], $_POST['year'],
				  $_POST['country-name'], $_POST['institution'],
				  $pay_url,$pay_url), 
				  HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
				  
				  mail( ADMINISTRATOR_EMAIL, 
				  USER_ORDER_SUBJECT,
				  sprintf(USER_ORDER_MESSAGE, 
				  $_POST['first_name'], $_POST['last_name'],
				  date('d/m/Y - H:m'),
				  $_POST['first_name'], $_POST['last_name'],
				  $_POST['email'], $_POST['year'],
				  $_POST['country-name'], $_POST['institution'],
				  $pay_url,$pay_url), 
				  HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
				  
				  # PayPal request
			
				$paypal = new SetExpressCheckout("".$_SESSION['order']['price']."");	
				$paypal->setNVP("DESC", "12th world Congress of Semiotics Sofia 2014, 16-20 September. New Bulgarian University(Registration #".number_pad($order_id,5).")");
							
				$paypal->getResponse();	
			}
				  
		   
			
		}
		
		
		
	}
	
}



$sql = "SELECT
			 
			  d.title,
			  d.meta_description,
			  d.description,
			  d.meta_key
		  FROM
			  pages AS c,
			  page_description AS d
		  WHERE
			c.page_id = d.page_id
			AND
			d.lang_id = '".$_SESSION['lang_id']."'
			AND
			c.sef_url = 'registration-form' 
			
		  ";
$result = query($sql);
$row = mysql_fetch_object($result);


head(sth($row->title),sth($row->meta_description),sth($row->meta_key));

if($_GET['prm2'])
{
	$order_id = intval(base64_decode($_GET['prm2']));
			$sql = "SELECT
					firstname,
					lastname,
					email,
					country,
					continent,
					institution,
					status,
					iass,
					culinary,
					dinner,
					allergic,
					byear
					
				FROM
					orders
				WHERE
				    order_id ='$order_id'
					
					";
			$result_order = query($sql);
			$row_order = mysql_fetch_object($result_order);
			
			$_SESSION['USER_EMAIL'] = $row_order->email;
			$_SESSION['first_name'] = $row_order->firstname;
	        $_SESSION['last_name'] = $row_order->lastname;
			$_SESSION['country-name'] = $row_order->country;
			$_SESSION['country'] = $row_order->continent;
}

?>
<div id="right">
			<div class="header">
				<h1><?=__New_Semiotics?></h1>
				<h2><?=__Between_Tradition?></h2>
			</div>
			
			<div id="content" >
            
            
            
            
			<h3><?=stripslashes($row->title)?></h3> 
				
				<div id="lang" style="background:none; margin:0; padding:0;">
				
                <?php
                $sql = 'SELECT lang_id,Name,code FROM language WHERE published';
				$result_lang = query($sql);
				
				$hiper = '';
				if($_GET['prm1']) $hiper .= '/'.$_GET['prm1'];
				if($_GET['prm2']) $hiper .= '/'.$_GET['prm2'];
		
				while ($row_lang = mysql_fetch_object($result_lang)) {
						$act = '';
						if($row_lang->lang_id == $_SESSION['lang_id']) $act = 'class="active"';
						
						echo '<a '.$act.' title="'.$row_lang->Name.'" href="'.HTML_BASE.$row_lang->code.$hiper.'"><img src="'.HTML_BASE.'images/'.$row_lang->code.'.gif"><br>'.$row_lang->code.'</a>';
										
				}
				
				?>
                
			</div><br />

                
                <?php if($message){?>
                <div class="message">
                <h2><?=$message?></h2>
                </div>	
                <?php }else{?>
				<form id="reg-form" action="<?=HTML_BASE.$_SESSION['lang']?>/registration-form" method="post" enctype="multipart/form-data">
                <input type="hidden" name="is_submitted"  value="1">
				<input type="hidden" id="to-date" value="<?=date("Y-m-d")?>">
                <input type="hidden" name="priceE"  value="">
                <input type="hidden" name="priceD" value="">
                <input type="hidden" name="country-name" value="">
                
                <?php if($_GET['prm2'] and $order_id){?>
                <input type="hidden" name="update" value="<?=$order_id?>">
                <?php }?>
				<div id="price" style=" position:fixed; top:200; left:50%; margin-left:200px; ">
					<h5><?=__Participation?></h5>
					<div>&euro; <span id="priceE">---</span></div>
					($ <span id="priceD">---</span>)<br>
					
                    <a target="_blank"  href="<?=HTML_BASE.$_SESSION['lang']?>/information" ><?=_Information?></a>
                    
				</div>
				
				<br><br>
				
				<?=__First_Name?><br>
				<input <?php if($row_order->firstname){?>disabled="disabled"<?php }?> type="text" id="fname" name="first_name" value="<?=$row_order->firstname?>"><br><br>
				
				<?=__Last_Name?><br>
				
                <input <?php if($row_order->lastname){?>disabled="disabled"<?php }?> type="text" id="lname" name="last_name" value="<?=$row_order->lastname?>"><br><br>
				
				E-mail:<br>
				<input <?php if($row_order->email){?>disabled="disabled"<?php }?> type="text" id="email" name="email" value="<?=$row_order->email?>"><br><br>
				
				<?=__Birth_Year?><br>
				<select <?php if($row_order->byear){?>disabled="disabled"<?php }?> id="year_select" name="year">
                <option value="">--<?=__Choise?>--</option>
                <?php 
				
				for($y = 1920; $y < 2000; $y ++)
				{
					$checked = '';
					if($row_order->byear == $y) $checked = 'selected="selected"';
				?>
				<option <?=$checked?>  value="<?=$y?>"><?=$y?></option>
				<?php }?>
				</select><br><br>
				
				<?=__Country?><br>
				<select <?php if($row_order->country){?>disabled="disabled"<?php }?> id="country_select" name="country">
                <option value="">--<?=__Choise?>--</option>
				<?php 
				
				$sql = "SELECT
						name,
						zone
						FROM
						countries
						
						ORDER BY name
					  ";
				$result = query($sql);
				
				 while ($row = mysql_fetch_object($result)) {
					$checked = '';
					if($row_order->country == $row->name) $checked = 'selected="selected"';
				?>
				<option <?=$checked?> data-title="<?=$row->name?>" value="<?=$row->zone?>"><?=$row->name?></option>
				<?php }?>
				</select><br><br>
				
				<?=__Institution?><br>
				<input <?php if($row_order->institution){?>disabled="disabled"<?php }?> id="institution" type="text" name="institution" value="<?=$row_order->institution?>"><br><br>
                
                
              <div style="width:300px;">
                <label>
                <?php
					$checked = '';
					if($row_order->iass) $checked = 'checked="checked"';
				?>
				<input <?=$checked?> type="checkbox"  name="iass"  value="1">
				<?=__Check_IASS?></label><br><br>
                
                <label>
				 <?php
					$checked = '';
					if($row_order->culinary) $checked = 'checked="checked"';
				?>
                <input <?=$checked?> type="checkbox"  name="culinary" value="1">
                <?=__Check_culinary?>
				</label><br><br>
                
                <label>
                <?php
					$checked = '';
					if($row_order->dinner) $checked = 'checked="checked"';
				?>
				<input <?=$checked?> type="checkbox"  name="dinner" value="1">
               <?=__Check_dinner?>
				</label><br><br>
                
                <label>
                <?php
					$checked = '';
					if($row_order->allergic) $checked = 'checked="checked"';
				?>
                <input <?=$checked?> type="checkbox"  name="allergic" value="1">
                <?=__Check_allergies?>
				</label><br><br>
			</div>
				
                
                <label>
				<input type="checkbox" id="agree" name="agree" value="1">
				<span id="agr-mess"><?=__I_agree?> <a href="<?=HTML_BASE.$_SESSION["lang"]?>/terms-and-conditions" target="_blank"><?=__Terms?></a></span></label><br><br>
				
				  <?php 
				  	if($_GET['prm2'] and $order_id){
						if($row_order->status == 2){
						?>
                        <?=__is_paid?>
                        <?php	
						}else{
					?>
                        <a class="button" id="send-button"  ><?=__Pay_now?></a>
                    <?php		
						}
					}else{
						?>
                     <a class="button" id="send-button"  ><?=__Register?></a>   
					<?php	
					}
				  
				  ?>
                   
                   <br />
				   <br />

				<img src="<?=HTML_BASE?>images/payment-paypal.jpg">
				<img src="<?=HTML_BASE?>images/payment-visa.jpg">
				<img src="<?=HTML_BASE?>images/payment-mc.jpg">
				<img src="<?=HTML_BASE?>images/payment-amex.jpg">
				<br><br>
				
								
				</form>
                
                
                <script>
				$(document).ready(function() {
					
					function IsEmail(email) {
					  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
					  return regex.test(email);
					}
							
					
					$('#send-button').click(function()
					{
						
							$('#reg-form input,select,textarea').removeClass('redborder');
							var error = 0;
							if(!$("#fname").val())
							{
								
								$("#fname").addClass('redborder');
								error = 1;
							}
							
							if(!$("#lname").val())
							{
								$("#lname").addClass('redborder');
								error = 1;
							}
							
							if(!IsEmail($("#email").val()))
							{
								$("#email").addClass('redborder');
								error = 1;
							}
							
							if(!$("#country_select").val())
							{
								$("#country_select").addClass('redborder');
								error = 1;
							}
							
							if(!$("#year_select").val())
							{
								$("#year_select").addClass('redborder');
								error = 1;
							}
							
							if(!$("#institution").val())
							{
								$("#institution").addClass('redborder');
								error = 1;
							}
							
							if(!$("#agree").is(':checked'))
							{
								//alert('Трябва да се съгласите с общите условия!');
				
								$("#agr-mess").css('color','#900');
								error = 1;
							}
							
							if(!error)
							{
								
								$('#reg-form').submit();
							}
							
					});
					
					
					   var priceE = Array([[120,100],[100,80],[80,70]],
										  [[140,120],[120,100],[100,90]],
										  [[220,200],[180,160],[160,140]]);
										  
						var priceD = Array([[170,140],[140,110],[110,100]],
										  [[200,170],[170,140],[140,130]],
										  [[310,280],[250,220],[220,200]]);
						
					
					$('#country_select').change(function(){calculate()});
					$('#year_select').change(function(){calculate()});
					
					$("[name='iass']").change(function(){calculate()});
					$("[name='culinary']").change(function(){calculate()});
					$("[name='dinner']").change(function(){calculate()});
					
					function calculate() {
						
						var time = 0;				  
						//var early = new Date(2014,2,1);
						var regular = new Date(2014,2,1);
						var late = new Date(2014,6,4);
						
						var additional = Array(0,0)
						if($("[name='iass']").is(':checked'))
						{
							
							if($("#country_select").val() == 3  )
							{
								additional[0] += 15;
								additional[1] += 20;
								if($('#country_select :selected').text() == 'ARGENTINA'){
									additional[0] += 10;
								    additional[1] += 15;
									
								}
								
								if( $('#country_select :selected').text() == 'VENEZUELA, BOLIVARIAN REPUBLIC OF' )
								{
									additional[0] += 10;
								    additional[1] += 15;
									
									
								}
								
								
							}else{
								additional[0] += 25;
								additional[1] += 35;
							}
												
						}
						
						if($("[name='culinary']").is(':checked'))
						{
							additional[0] += 35;
							additional[1] += 50;					
						}
						
						if($("[name='dinner']").is(':checked'))
						{
							additional[0] += 35;
							additional[1] += 50;					
						}
						
						
						var tarr = $('#to-date').val().split('-');
						
						var today = new Date(Number(tarr[0]),Number(tarr[1])-1,Number(tarr[2]));
						
						if(today > regular) time = 1;
						if(today > late) time = 2;
								  
						var year = 0;
						var country = $('#country_select').val() - 1;
						
						if($('#country_select :selected').text() == 'ARGENTINA' || $('#country_select :selected').text() == 'VENEZUELA, BOLIVARIAN REPUBLIC OF'  )
							{
								country = 1;
								
							}
							
							
						
						if ($('#year_select').val() >= 1984) year = 1;			
							
							
							
							$('#priceE').text((priceE[time][country][year]+additional[0]));
							$('#priceD').text(priceD[time][country][year]+additional[1]);
							$("[name='priceE']").val((priceE[time][country][year]+additional[0]));
							$("[name='priceD']").val(priceD[time][country][year]+additional[1]);
							
							$("[name='country-name']").val($('#country_select :selected').text());
							
						
								
					}
								
					 <?php if($_GET['prm2'] and $order_id){?>
                			calculate();
                	 <?php }?>
                });
				</script>
                
         <?php } ?>       
	</div>
<?php

foot();
?>